/**
 * @Title: model.js
 * @Description: model
 * @author: Fanchao
 * @date: 2020/8/29 11:21 下午
 * @version V1.0
 */

import { pathToRegexp } from 'path-to-regexp';
import { message } from 'antd';
import { getPagedEvaluateObjectModelOfTeacher, getTeacherEvaluateLineChart } from 'api/index';

export default {
    namespace: 'myEvaluations',
    state: {
        dataSource: {},
        reqPageInfo: {
            pageNumber: 1,
            pageSize: 10,
        },
        lineData: [],
        userData: {},
    },
    reducers: {
        setDataSource(state, { payload: { dataSource, reqPageInfo } }) {
            return {
                ...state,
                dataSource,
                reqPageInfo,
            };
        },
        setLineData(state, { payload: lineData }) {
            return { ...state, lineData };
        },
        setUserData(state, { payload: userData }) {
            return { ...state, userData };
        },
    },
    effects: {
        /**
         * @desc 我的历史评价列表
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getDataSource({ payload }, { call, put, select }) {
            const result = yield call(getPagedEvaluateObjectModelOfTeacher, payload);
            if (result.status === 'success') {
                const dataSource = result.result;
                yield put({
                    type: 'setDataSource',
                    payload: {
                        dataSource,
                        reqPageInfo: payload.reqPageInfo,
                    },
                });
            } else {
                message.error(result.message);
            }
        },
        /**
         * @desc 折线统计
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {Generator<*, void, *>}
         */* getLineData({ payload }, { call, put, select }) {
            const result = yield call(getTeacherEvaluateLineChart, payload);
            if (result.status === 'success') {
                const lineData = result.result.evaluateLineChartList;
                const userData = result.result.teacherUserModel;
                yield put({
                    type: 'setLineData',
                    payload: lineData,
                });
                yield put({
                    type: 'setUserData',
                    payload: userData,
                });
            } else {
                message.error(result.message);
            }
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname }) => {
                if (pathToRegexp('/teaching-quality/my-evaluations').exec(pathname)) {
                    dispatch({
                        type: 'getDataSource',
                        payload: {
                            reqPageInfo: {
                                pageNumber: 1,
                                pageSize: 10,
                            },
                        },
                    });
                    dispatch({
                        type: 'getLineData',
                        payload: {},
                    });
                }
            });
        },
    },
};
